﻿using System.Collections.Generic;
using System.Windows.Input;
using Wen.Core;
using Snoop;

namespace Wen.UI.Main
{
	internal class ProcessListViewModel : ViewModel
	{
		private IList<WindowInfo> _processes;
		public IList<WindowInfo> Processes {
			get { return _processes; }
			set {
				_processes = value;
				OnPropertyChanged("Processes");
			}
		}

		public ICommand SelectProcessCommand { get; private set; }
		public ICommand UpdateCommand { get; private set; }

		public ProcessListViewModel() {
			UpdateProcesses();
			UpdateCommand = new SimpleCommand(UpdateProcesses);
			SelectProcessCommand = new SimpleCommand<WindowInfo>(SelectedProcessCommandImpl, SelectProcessCommandCanExecute);
		}

		private bool SelectProcessCommandCanExecute(WindowInfo process) {
			return process != null;
		}

		private void SelectedProcessCommandImpl(WindowInfo process) {
			Injector.Launch(process.HWnd, typeof(NavigationWindow).Assembly, typeof(NavigationWindow).FullName, "Go");
			NavigationWindow.Instance.CloseWindow();
		}

		private void UpdateProcesses() {
			Processes = ProcessHelpers.GetRunningWpfWindows();
		}
	}
}
